package com.sixkey.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * ClassName: Response
 * Package: com.sixkey.response
 * Description:
 *
 * @Author: @weixueshi
 * @Create: 2023/9/27 - 17:13
 * @Version: v1.0
 */

/**
 * 响应类
 */
@Data
@AllArgsConstructor
public class Response<T> implements Serializable {

    /**
     * 状态码
     */
    protected int code;

    /**
     * 响应信息
     */
    protected String message;

    /**
     * 返回数据
     */
    private T data;

    public static <T> Response<T> success() {
        return new Response<>();
    }

    public static <T> Response<T> success(T data) {
        return new Response<>(data);
    }

    public static <T> Response<T> error(String message) {
        return new Response<T>(ResponseEnum.ERROR,message);
    }

    public static <T> Response<T> error(int code ,String message) {
        return new Response<T>(code,message);
    }

    public static <T> Response<T> error() {
        return new Response<T>(ResponseEnum.ERROR);
    }

    public Response() {
        this.code = ResponseEnum.SUCCESS.getCode();
        this.message = ResponseEnum.SUCCESS.getMessage();
    }

    public Response(ResponseEnum statusEnums) {
        this.code = statusEnums.getCode();
        this.message = statusEnums.getMessage();
    }

    public Response(ResponseEnum statusEnums,String message) {
        this.code = statusEnums.getCode();
        this.message = message;
    }

    /**
     * 若没有数据返回，可以人为指定状态码和提示信息
     */
    public Response(int code, String msg) {
        this.code = code;
        this.message = msg;
    }

    /**
     * 有数据返回时，状态码为200，默认提示信息为“操作成功！”
     */
    public Response(T data) {
        this.data = data;
        this.code = ResponseEnum.SUCCESS.getCode();
        this.message = ResponseEnum.SUCCESS.getMessage();
    }

    /**
     * 有数据返回，状态码为 200，人为指定提示信息
     */
    public Response(T data, String msg) {
        this.data = data;
        this.code = ResponseEnum.SUCCESS.getCode();
        this.message = msg;
    }
}
